﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "datasourcegdaldem.h"
#include "datasourcetiles.h"
#include "Extent.h"
#include "TileKey.h"

#include <qDebug>
#include <QFileDialog>
#include <QProcess>
#include <qmath.h>
#include <QMessageBox>
#include <QProgressDialog>

#include "osgDB/ReadFile"
#include "datasourceterrain.h"
mainwindow::mainwindow(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::mainwindow)
{
    ui->setupUi(this);

    //_source_in = new rxtiler::DataSourceGDAL("");
    //_source_out = new rxtiler::DataSourceTerrain();
    //_datasourceTiles=new rxtiler::DataSourceTerrain();

    this->titleoption = rxtiler::TileOption::outTileOption();

}

mainwindow::~mainwindow()
{
    delete ui;
}

void mainwindow::on_OpenFile_clicked()
{
    if( ui->FileType1->isChecked() && !ui->FileType2->isChecked() )
    {
        //影像
        QString filename = QFileDialog::getOpenFileName(this,
                           tr("选择图像"),
                           "",
                           tr("IMG (*.tif *.tiff *.TIF *.TIFF)")); //选择路径
        if( filename.isEmpty() )
        {
            return;
        }
        else
        {
            ui->FilePathshow->setText( filename );
            dataSource = new rxtiler::DataSourceGDAL(ui->FilePathshow->text());
            pExtent = dataSource->getExtent();
            ui->Left->setText(QString::number(pExtent.left,10,8));
            ui->Top->setText(QString::number(pExtent.top,10,8));
            ui->Right->setText(QString::number(pExtent.right,10,8));
            ui->Bottom->setText(QString::number(pExtent.bottom,10,8));
        }
    }
    if( !ui->FileType1->isChecked() && ui->FileType2->isChecked() )
    {
        //DEM
        QString filename = QFileDialog::getOpenFileName(this,
                           tr("选择DEM"),
                           "",
                           tr("DEM (*.tif *.tiff *.TIF *.TIFF)")); //选择路径
        if( filename.isEmpty() )
        {
            return;
        }
        else
        {
            ui->FilePathshow->setText( filename );
            dataSourceDEM = new rxtiler::DataSourceGDALDEM();

            pExtent = dataSourceDEM->getExtent();
            ui->Left->setText(QString::number(pExtent.left,10,8));
            ui->Top->setText(QString::number(pExtent.top,10,8));
            ui->Right->setText(QString::number(pExtent.right,10,8));
            ui->Bottom->setText(QString::number(pExtent.bottom,10,8));
        }
    }
}

void mainwindow::on_Calculation_clicked()
{
}

void mainwindow::on_SaveFile_clicked()
{
    QString file_dir = QFileDialog::getExistingDirectory(this,
                   "选择保存路径",
                   "./",
                    QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
    if( file_dir.isEmpty() )
    {
        return;
    }
    else
    {
        ui->SavePathShow->setText( file_dir );
    }

    if( ui->FileType1->isChecked() && !ui->FileType2->isChecked() ){
        _source_out_img = new rxtiler::DataSourceTiles();
        _source_out_img->setPath(file_dir);
    }
    else if( !ui->FileType1->isChecked() && ui->FileType2->isChecked() ){
        _source_out = new rxtiler::DataSourceTerrain();
        _source_out->setPath(file_dir);
    }


}

void mainwindow::on_structure_clicked()
{
    if(ui->SavePathShow->text().isEmpty() || ui->FilePathshow->text().isEmpty())
        return;
    if(ui->txtMinLevel->text().toInt() >= ui->txtMaxLevel->text().toInt())
        return;
    if(ui->Left->text().isEmpty() || ui->Right->text().isEmpty() ||
            ui->Top->text().isEmpty() || ui->Bottom->text().isEmpty())
        return;
    if(ui->Left->text().toDouble() >= ui->Right->text().toDouble() ||
            ui->Bottom->text().toDouble() >= ui->Top->text().toDouble() )
        return;

    bool isdem = false;
    if( !ui->FileType1->isChecked() && ui->FileType2->isChecked() ){
        isdem = true;
    }


    int tilesize = ui->lineEdit_tilesize->text().toInt();
    if(tilesize > 0){
        rxtiler::TileOption::outTileOption()->tilesize = tilesize;
    }


    rxtiler::Extent nowTif( ui->Left->text().toDouble(),
                            ui->Right->text().toDouble(),
                            ui->Bottom->text().toDouble(),
                            ui->Top->text().toDouble());
    if(!isdem){
        dataSource->setNowLrbt(nowTif);
    }
    else{
        nowTif = dataSourceDEM->getExtent();
    }

    int lodMin = ui->txtMinLevel->text().toInt();
    int lodMax = ui->txtMaxLevel->text().toInt();


    std::vector<rxtiler::TileKey> tiles;

    for(int lod = lodMin; lod <= lodMax; ++lod){
        int x0 = titleoption->getTileX(lod,nowTif.left);
        int x1 = titleoption->getTileX(lod,nowTif.right);
        int y0 = titleoption->getTileY(lod,nowTif.bottom);
        int y1 = titleoption->getTileY(lod,nowTif.top);

        if(y0 > y1){
            int t = y0;
            y0 = y1;
            y1 = t;
        }

        for(int x = x0; x <= x1; x++)
        {
            for(int y = y0; y <=y1; y++)
            {
                tiles.push_back(rxtiler::TileKey(x,y,lod));
            }
        }
    }

    if(tiles.size() <= 0){
        QMessageBox::information(this,"提示","瓦片集合为空");
        return;
    }
    int length = tiles.size();

    QProgressDialog dlg;
    dlg.setRange(0,length);
    dlg.setModal(true);
    dlg.setCancelButtonText("取消");
    dlg.setWindowTitle("loading...");
    dlg.show();

    //图像
    if( ui->FileType1->isChecked() && !ui->FileType2->isChecked() )
    {
        osg::ref_ptr<osg::Image> img;
        for(int i = 0; i < length; ++i)
        {
            rxtiler::TileKey key = tiles[i];
            rxtiler::Extent ext = this->titleoption->getExtent(key);
            img = this->dataSource->getImage(key,ext);
            if(img){
                this->_source_out_img->saveImage(key,img.get());
            }


            dlg.setValue(i + 1);
            if(dlg.wasCanceled()){
                break;
            }
        }
    }
    //DEM
    if( !ui->FileType1->isChecked() && ui->FileType2->isChecked() )
    {
        osg::ref_ptr<osg::HeightField> dem;
        for(int i = 0; i < length; ++i)
        {
            rxtiler::TileKey key = tiles[i];
            rxtiler::Extent ext = this->titleoption->getExtent(key);
            osg::HeightField* dem = this->dataSourceDEM->getDEM(key,ext);
            if(dem){
                this->_source_out->saveDEM(key,dem);
            }

            dlg.setValue(i + 1);
            if(dlg.wasCanceled()){
                break;
            }
        }
    }
}

void mainwindow::on_close_clicked()
{
    this->close();
}
